package com.qinggeng.project.api.service.impl;

import com.qinggeng.common.constant.UserConstants;
import com.qinggeng.common.enums.UserTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.project.api.mapper.ApiMemberMapper;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.api.service.IApiMemberService;
import com.qinggeng.project.system.domain.SysUser;
import com.qinggeng.project.system.domain.UserWallet;
import com.qinggeng.project.system.mapper.UserWalletMapper;
import com.qinggeng.project.system.service.IUserAccountService;
import com.qinggeng.project.tool.sms.service.ISysSmsService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 会员业务处理
 */
@Service
public class ApiMemberServiceImpl implements IApiMemberService {
    @Resource
    private ApiMemberMapper memberMapper;
    @Resource
    private UserWalletMapper userWalletMapper;
    @Autowired
    private ISysSmsService smsService;
    @Autowired
    private IUserAccountService userAccountService;

    @Override
    public SysUser selectByPhone(String phone, String userType) {
        return memberMapper.selectMemberByPhone(phone,userType);
    }

    @Override
    public List<SysUser> listMember() {
        return memberMapper.listMember();
    }

    @Override
    public SysUser selectMemberById(Long userId) {
        return memberMapper.selectMemberById(userId);
    }

    @Override
    public SysUser insertPhone(String phone, String passWord, String pointName, String userType) {
        SysUser user = new SysUser();
        user.setUserType(userType);
        //设置用户名，如果用户类型为果果专员，用户名默认为配送点名称
        String userName=phone;
        if(user.getUserType().equals(UserTypeEnum.COURIER.getCode())){
            userName=pointName;
        }
        user.setUserName(userName);
        user.setPhonenumber(phone);
        if (StringUtils.isNotEmpty(passWord)){
            user.setPassword(ApiUtils.encryptPassword(passWord));
            user.setPwdSet("20");
        }else{
            user.setPwdSet("10");
        }
        user.setNickName(phone.substring(0,3) + "****"+phone.substring(7,11));
        if(userType.equals(UserTypeEnum.COURIER.getCode())){
            user.setAvatar(UserConstants.COURIER_AVATAR);
        }
        if(memberMapper.insertMember(user)<=0){
            throw new CustomException("注册失败");
        }
        //检查账户
        userAccountService.checkFilterUserAccount(user.getUserId());

        UserWallet userWallet  =new UserWallet();
        userWallet.setUserId(user.getUserId());
        userWalletMapper.insertUserWallet(userWallet);
        //如果用户类型为果果专员，则发送初始密码短信
        if(user.getUserType().equals(UserTypeEnum.COURIER.getCode())){
            try {
                smsService.sendInitPassword(user.getPhonenumber(),passWord);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return user;
    }

    @Override
    public int updatePassWord(String phone, String password, String userType) {
        SysUser user = memberMapper.selectMemberByPhone(phone, userType);
        user.setPassword(ApiUtils.encryptPassword(password));
        user.setPwdSet("20");
        return memberMapper.updateMember(user);
    }

    @Override
    public int updatePhone(Long userId, String phone) {
        SysUser user = memberMapper.selectMemberById(userId);
        user.setPhonenumber(phone);
        return memberMapper.updateMember(user);
    }


    @Override
    public int updateMember(SysUser sysUser) {
        return memberMapper.updateMember(sysUser);
    }

    @Override
    public int deleteSysUserByUserId(Long userId) {
        return memberMapper.deleteSysUserByUserId(userId);
    }
}
